package main

import (
    "crypto/md5"
    "fmt"
    "io"
    "math"
    "os"
)

const filechunk = 8192 // we settle for 8KB

func main() {
	
    //file, err := os.Open("/Users/liguanghui/Documents/IIT/IIT-ECE449.ova")
   
	file, err := os.Open("/Users/liguanghui/Documents/资料/立志励志/admaid_visit_campus_800x650.jpg")
	
    if err != nil {
        panic(err.Error())
    }

    defer file.Close()
    // calculate the file size
    info, _ := file.Stat()
	filesize := info.Size()
	fmt.Println(filesize)
    blocks := uint64(math.Ceil(float64(filesize) / float64(filechunk)))
    hash := md5.New()

    for i := uint64(0); i < blocks; i++ {
        blocksize := int(math.Min(filechunk, float64(filesize-int64(i*filechunk))))
        buf := make([]byte, blocksize)
        file.Read(buf)
        io.WriteString(hash, string(buf)) // append into the hash
	}

    fmt.Println(string(hash.Sum(nil)))
    fmt.Printf("%s checksum is %x\n", file.Name(), hash.Sum(nil))
}
